﻿using System;
using System.Linq;
using DBProvider;
using DBProvider.Providers;
using System.Collections.Generic;

namespace HealthCommunity.Controls
{
	public partial class UCActivityEdit : System.Web.UI.UserControl
	{
		private IActivityProvider _ActivityProvider;
		private List<DrugStore> lstDrugStore;

		private Activity _CurrentActivity;

		public int ActivityID
		{
			get
			{
				var id = 0;

				if(ViewState["ActivityID"] != null)
				{
					id = (int)ViewState["ActivityID"];
				}
				else
				{
					id = CommonHelper.QueryStringInt("activityid");
				}

				return id;
			}
			set
			{
				ViewState["ActivityID"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindActivityCategory();
				BindDrugStore();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindActivity();
		}

		protected void ddlDrugStore_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindMember();
		}

		protected void btnRemove_Click(object sender, EventArgs e)
		{
			if(lstMemberSelected.SelectedItem != null)
			{
				lstMemberSelected.Items.Remove(lstMemberSelected.SelectedItem);
			}
		}

		protected void btnAdd_Click(object sender, EventArgs e)
		{
			if(lstMember.SelectedItem != null)
			{
				var item = new System.Web.UI.WebControls.ListItem(lstMember.SelectedItem.Text, lstMember.SelectedItem.Value);

				if(!lstMemberSelected.Items.Contains(item))
				{
					lstMemberSelected.Items.Add(item);
				}
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			var theActivity = new Activity();

			if(_ActivityProvider == null)
			{
				_ActivityProvider = new ActivityProvider();
			}

			if(this.ActivityID > 0)
			{
				theActivity = _ActivityProvider.GetActivity(this.ActivityID);
			}

			theActivity.ActivityAddress = txtActivityAddress.Text.Trim();
			var selectedActCat = ddlActivityCategory.SelectedValue;
			if(!string.IsNullOrEmpty(selectedActCat))
			{
				theActivity.ActivityCategoryID = int.Parse(selectedActCat);
			}
			theActivity.ActivityContactPerson = txtActivityContactPerson.Text.Trim();
			theActivity.ActivityContactTel = txtActivityContactTel.Text.Trim();
			theActivity.ActivityDescription = txtActivityDescription.Text.Trim();
			theActivity.ActivityTitle = txtActivityTitle.Text.Trim();
			if(!string.IsNullOrEmpty(txtActStartDate.Text))
			{
				DateTime start = new DateTime();
				if(DateTime.TryParse(txtActStartDate.Text.Trim(), out start))
				{
					theActivity.ActivityStartTime = start;
				}
			}
			if(!string.IsNullOrEmpty(txtActEndDate.Text))
			{
				DateTime end = new DateTime();
				if(DateTime.TryParse(txtActEndDate.Text.Trim(), out end))
				{
					theActivity.ActivityEndTime = end;
				}
			}
			if(!string.IsNullOrEmpty(txtDeadlineTime.Text))
			{
				DateTime deadLine = new DateTime();
				if(DateTime.TryParse(txtDeadlineTime.Text.Trim(), out deadLine))
				{
					theActivity.DeadlineTime = deadLine;
				}
			}

			if(chbIsPublic.Checked)
			{
				theActivity.IsPublic = 1;
			}
			else
			{
				theActivity.IsPublic = 0;
			}

			if(this.ActivityID <= 0)
			{
				var user = SiteContext.Current.CurrentUser;

				if(user != null)
				{
					theActivity.ActivityInitiator = user.UserID;
					theActivity.DrugStoreID = user.DrugStoreID;
				}

				var msg = string.Empty;
				try
				{
					theActivity = _ActivityProvider.CreateActivity(theActivity, out msg);
					if(theActivity != null)
					{
						if(!chbIsPublic.Checked)
						{
							List<int> lstSelectedDrugStoreIDs = new List<int>();
							foreach(System.Web.UI.WebControls.ListItem item in chblstDrugStore.Items)
							{
								if(item.Selected)
								{
									lstSelectedDrugStoreIDs.Add(int.Parse(item.Value));
								}
							}

							List<RLT_ActivityAndShareTo> lstNewActivityAndShareTo = new List<RLT_ActivityAndShareTo>();

							if(lstSelectedDrugStoreIDs.Count > 0)
							{
								foreach(var id in lstSelectedDrugStoreIDs)
								{
									var rltActivityAndShareTo = new RLT_ActivityAndShareTo() { ActivityID = theActivity.ActivityID, ShareToDrugStoreID = id };
									lstNewActivityAndShareTo.Add(rltActivityAndShareTo);
								}
							}

							if(lstMemberSelected.Items.Count > 0)
							{
								foreach(System.Web.UI.WebControls.ListItem item in lstMemberSelected.Items)
								{
									var memberID = int.Parse(item.Value);
									var rltActivityAndShareTo = new RLT_ActivityAndShareTo() { ActivityID = theActivity.ActivityID, ShareToMemberID = memberID };
									lstNewActivityAndShareTo.Add(rltActivityAndShareTo);
								}
							}

							if(lstNewActivityAndShareTo.Count > 0)
							{
								_ActivityProvider.CreateActivityAndShareTo(lstNewActivityAndShareTo);
							}
						}

						this.ActivityID = theActivity.ActivityID;
						ltrMsg.Text = "保存成功";
					}
					else
					{
						ltrMsg.Text = msg;
					}
				}
				catch(Exception exc)
				{
					ltrMsg.Text = exc.Message;
				}
			}
			else
			{
				if(_ActivityProvider.UpdateActivity(theActivity))
				{
					if(!chbIsPublic.Checked)
					{
						List<int> lstSelectedDrugStoreIDs = new List<int>();
						foreach(System.Web.UI.WebControls.ListItem item in chblstDrugStore.Items)
						{
							if(item.Selected)
							{
								lstSelectedDrugStoreIDs.Add(int.Parse(item.Value));
							}
						}

						List<RLT_ActivityAndShareTo> lstActivityAndShareTo = new List<RLT_ActivityAndShareTo>();

						if(lstSelectedDrugStoreIDs.Count > 0)
						{
							foreach(var id in lstSelectedDrugStoreIDs)
							{
								var rltActivityAndShareTo = new RLT_ActivityAndShareTo() { ActivityID = this.ActivityID, ShareToDrugStoreID = id };
								lstActivityAndShareTo.Add(rltActivityAndShareTo);
							}
						}

						if(lstMemberSelected.Items.Count > 0)
						{
							foreach(System.Web.UI.WebControls.ListItem item in lstMemberSelected.Items)
							{
								var memberID = int.Parse(item.Value);
								var rltActivityAndShareTo = new RLT_ActivityAndShareTo() { ActivityID = this.ActivityID, ShareToMemberID = memberID };
								lstActivityAndShareTo.Add(rltActivityAndShareTo);
							}
						}

						_ActivityProvider.SaveActivityAndShareTo(this.ActivityID, lstActivityAndShareTo);
					}

					ltrMsg.Text = "保存成功";
				}
				else
				{
					ltrMsg.Text = "保存失败了";
				}
			}
		}

		private void BindDrugStore()
		{
			chblstDrugStore.Items.Clear();
			chblstDrugStore.DataSource = GetDrugStore();
			chblstDrugStore.DataBind();

			ddlDrugStore.Items.Clear();
			ddlDrugStore.DataSource = GetDrugStore();
			ddlDrugStore.DataBind();

			BindMember();
		}

		private void BindMember()
		{
			if(!string.IsNullOrWhiteSpace(ddlDrugStore.SelectedValue))
			{
				var selectedDrugStoreID = int.Parse(ddlDrugStore.SelectedValue);

				var userProvider = new UserProvider();

				lstMember.DataSource = userProvider.GetUserByDrugStore(selectedDrugStoreID, true);
				lstMember.DataBind();
			}
		}

		private List<DrugStore> GetDrugStore()
		{
			List<DrugStore> result;

			if(this.lstDrugStore != null)
			{
				result = lstDrugStore;
			}
			else
			{
				var drugStoreProvider = new DrugStoreProvider();

				lstDrugStore = drugStoreProvider.GetAllDrugStore();

				result = lstDrugStore;
			}

			return result;
		}

		private void BindActivityCategory()
		{
			if(_ActivityProvider == null)
			{
				_ActivityProvider = new ActivityProvider();
			}

			ddlActivityCategory.Items.Clear();
			ddlActivityCategory.DataSource = _ActivityProvider.GetAllActivityCategory();
			ddlActivityCategory.DataBind();
		}

		private void BindActivity()
		{
			if(this.ActivityID > 0)
			{
				if(_ActivityProvider == null)
				{
					_ActivityProvider = new ActivityProvider();
				}

				if(_CurrentActivity == null)
				{
					_CurrentActivity = _ActivityProvider.GetActivity(this.ActivityID);
				}

				if(_CurrentActivity != null)
				{
					this.txtActEndDate.Text = _CurrentActivity.ActivityEndTime.GetValueOrDefault().ToShortDateString();
					this.txtActivityAddress.Text = _CurrentActivity.ActivityAddress;
					this.txtActivityContactPerson.Text = _CurrentActivity.ActivityContactPerson;
					this.txtActivityContactTel.Text = _CurrentActivity.ActivityContactTel;
					this.txtActivityDescription.Text = _CurrentActivity.ActivityDescription;
					this.txtActivityTitle.Text = _CurrentActivity.ActivityTitle;
					this.txtActStartDate.Text = _CurrentActivity.ActivityStartTime.GetValueOrDefault().ToShortDateString();
					this.txtDeadlineTime.Text = _CurrentActivity.DeadlineTime.GetValueOrDefault().ToShortDateString();
					this.txtMaximumPerson.Text = _CurrentActivity.MaximumPerson.GetValueOrDefault(0).ToString();
					foreach(System.Web.UI.WebControls.ListItem item in ddlActivityCategory.Items)
					{
						if(item.Value.Equals(_CurrentActivity.ActivityCategoryID.GetValueOrDefault(0).ToString()))
						{
							item.Selected = true;
						}
					}

					if(_CurrentActivity.IsPublic != 1)
					{
						var activityAndShareTo = _ActivityProvider.GetActivityAndShareToByActivity(_CurrentActivity.ActivityID);

						if(activityAndShareTo != null && activityAndShareTo.Count > 0)
						{
							var activityAndShareToDrugStore = activityAndShareTo.Where(vs => vs.ShareToDrugStoreID > 0);
							var activityAndShareToMember = activityAndShareTo.Where(vs => vs.ShareToMemberID > 0);

							if(activityAndShareToDrugStore != null)
							{
								var lstVideoShareToDrugStore = activityAndShareToDrugStore.ToList();

								if(lstVideoShareToDrugStore.Count > 0)
								{
									foreach(System.Web.UI.WebControls.ListItem item in chblstDrugStore.Items)
									{
										var drugStoreID = int.Parse(item.Value);

										item.Selected = lstVideoShareToDrugStore.Count(vs => vs.ShareToDrugStoreID == drugStoreID) > 0;
									}
								}
							}

							lstMemberSelected.Items.Clear();

							if(activityAndShareToMember != null)
							{
								var lstVideoShareToMember = activityAndShareToMember.ToList();

								if(lstVideoShareToMember.Count > 0)
								{
									var data = from vs in lstVideoShareToMember
														 select new { UserCName = vs.User.UserCName, ShareToMemberID = vs.ShareToMemberID };

									foreach(var dataItem in data)
									{
										var item = new System.Web.UI.WebControls.ListItem(dataItem.UserCName, dataItem.ShareToMemberID.ToString());

										//if (!lstMemberSelected.Items.Contains(item))
										//{
										lstMemberSelected.Items.Add(item);
										//}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}